package top.scsoul.boot.controller;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import top.scsoul.boot.bean.PageBean;
import top.scsoul.boot.bean.XhrResponse;
import top.scsoul.boot.entity.Customer;
import top.scsoul.boot.service.CustomerService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * (Customer)表控制层
 *
 * @author 曹波涛
 * @since 2021-06-10 20:45:20
 */
@Controller
@RequestMapping("customer")
public class CustomerController {
    Logger logger = Logger.getLogger(BaseController.class);
    /**
     * 服务对象
     */
    @Resource
    private CustomerService customerService;

    /**
     * 渲染客户列表页面
     *
     * @param currentPage
     * @param pageSize
     * @param model
     * @return
     */
    @RequestMapping
    public String customer(
            @RequestParam(required = false) Integer currentPage,
            @RequestParam(required = false) Integer pageSize,
            Model model) {
        if (currentPage == null || currentPage < 1) {
            currentPage = 1;
        }
        if (pageSize == null || pageSize < 1) {
            pageSize = 5;
        }
        List<Customer> customers = customerService.queryAllList(currentPage, pageSize);
        PageBean pageBean = PageBean.createPageBean(currentPage, pageSize, customerService.count());
        model.addAttribute("customers", customers);
        model.addAttribute("pageBean", pageBean);
        System.out.println(pageBean);
        return "customer";
    }

    @RequestMapping("queryLike")
    public String queryCustomerLike(Customer customer,
                                    Model model) {
        customer.setCustName(customer.getCustName().trim());
        List<Customer> customers = customerService.queryByLike(customer);
        model.addAttribute("customers", customers);
        return "customer";
    }

    /**
     * 添加客户
     *
     * @param customer
     * @param result
     * @return
     */
    @ResponseBody
    @RequestMapping("add")
    public XhrResponse add(@Valid Customer customer, BindingResult result) {
        logger.info(customer);
        if (result.getFieldErrorCount() > 0) {
            List<FieldError> fieldErrors = result.getFieldErrors();
            ArrayList<String> arrayList = new ArrayList<>();
            for (FieldError fieldError : fieldErrors) {
                logger.info(fieldError.getField() + ":" + fieldError.getDefaultMessage());
                arrayList.add(fieldError.getDefaultMessage());
            }
            return XhrResponse.error(400, arrayList, "参数有误");
        }
        customerService.insert(customer);
        return XhrResponse.success();
    }

    @ResponseBody
    @RequestMapping("update")
    public XhrResponse update(@Valid Customer customer, BindingResult result) {
        logger.info(customer);
        if (result.getFieldErrorCount() > 0) {
            List<FieldError> fieldErrors = result.getFieldErrors();
            ArrayList<String> arrayList = new ArrayList<>();
            for (FieldError fieldError : fieldErrors) {
                logger.info(fieldError.getField() + ":" + fieldError.getDefaultMessage());
                arrayList.add(fieldError.getDefaultMessage());
            }
            return XhrResponse.error(400, arrayList, "参数有误");
        }
        customerService.update(customer);
        return XhrResponse.success();
    }

    @ResponseBody
    @RequestMapping("delete/{id}")
    public XhrResponse delete(@PathVariable("id") Integer id) {
        customerService.deleteById(id);
        return XhrResponse.success();
    }

    /**
     * 根据id获取客户
     *
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("{id}")
    public XhrResponse getOne(@PathVariable("id") Integer id) {
        Customer customer = customerService.queryById(id);
        logger.info(customer);
        return XhrResponse.success(customer);
    }

}